﻿using GodSoft.Common;
using GodSoft.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GodSoft.Controllers
{
    public class CollectorController : BaseController
    {
        GodSoftContext _context = new GodSoftContext();

        public ActionResult GetCollectorTypeComboboxList()
        {
            var list = from i in _context.DeviceTypes
                       orderby i.ID
                       select new
                       {
                           CollectorTypeID = i.ID,
                           CollectorTypeName = i.Type_Name
                       };

            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Regist(Collector Collector)
        {
            _context.Collectors.Add(Collector);
            _context.SaveChanges();
            return Content("OK");
        }

      
     
        public ActionResult GetAll()
        {
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);

           
                var temp = from i in _context.Collectors
                           from j in _context.DeviceTypes
                           where i.TypeID==j.ID
                           orderby i.ID
                           select new
                           {
                               i.ID,
                               i.Name,
                               i.TypeID,//采集器类型编号
                               j.Type_Name,//采集器类型名称
                               i.IP,
                               i.Port,
                               i.Collector_Heartbeat_Packets,
                               i.Heartbeat_Interval,
                               i.Registration_Packet,
                               i.Collector_Status,
                               i.Enable,
                               i.Notes
                              
                           };
                var list = temp.Skip((pageIndex - 1) * pageSize).Take(pageSize);
                var result = new { total = temp.Count(), rows = list };
                return Json(result, JsonRequestBehavior.AllowGet);
           
        }

        public ActionResult UpdateInfo(Collector Collector)
        {
            _context.Collectors.Attach(Collector);
            _context.Entry<Collector>(Collector).State = EntityState.Modified;
            _context.SaveChanges();
            return Content("OK");
        }

        public ActionResult DeleteInfo(string deleteInfoID)
        {
            if (string.IsNullOrEmpty(deleteInfoID))
            {
                return Content("请选择您要删除的数据");
            }
            //截取传递过来的字符串的数字信息
            var idsStr = deleteInfoID.Split(',');

            List<int> deleteIDList = new List<int>();

            foreach (var ID in idsStr)
            {
                deleteIDList.Add(Convert.ToInt32(ID));
            }
            if (DeleteManyInfoes(deleteIDList) > 0)
            {
                return Content("OK");
            }
            else
            {
                return Content("删除失败，请您检查");
            }

        }

        public int DeleteManyInfoes(List<int> DeleteIDList)
        {
            foreach (var id in DeleteIDList)
            {
                Collector Collector = _context.Collectors.Where(f => f.ID == id).FirstOrDefault();
                _context.Collectors.Remove(Collector);
            }
            return _context.SaveChanges();
        }
   

    }
}
